<?php

namespace App\Dao;

use App\Model\UserGoldLog;
use App\Constants\Model\UserGoldLog as UserGoldLogConstants;


class UserGoldLogDao extends AbstractDao
{
    protected $defaultFields = [];

    public function where(array $where): \Hyperf\Database\Model\Builder
    {
        $query = UserGoldLog::query();
        foreach ($where as $k => $v) {
            if (is_array($v)) {
                $query->where($k, $v[0], $v[1]);
            } else {
                $query->where($k, $v);
            }
        }
        return $query;
    }


    public function getCommissionSum(int $userId)
    {
        return $this->where(
            [
                'user_id' => $userId,
                'type' => UserGoldLogConstants::TYPE_COMMISSION,
                'account_type' => UserGoldLogConstants::ACCOUNT_TYPE_COMMISSION_INCOME
            ]
        )->sum('gold');
    }
}